import { login } from '../../api/login'
import { getUserInfo } from '../../api/mine'

const userModule = {
    namespaced: true,
    state:{
        userInfo: null,
        token:null,
    },
    getters:{
        token(state){
            return state.token
        },
        userInfo(state){
            return state.userInfo
        }
    },
    mutations:{
        SET_TOKEN(state, token){
            state.token = token
        },
        SET_USERINFO(state, userInfo){
            state.userInfo = userInfo
        }
    },
    
    actions:{
        // 异步登录逻辑
        async asyncLogin(ctx, {account, password}){
            const { status, token } = await login(account, password)
            // 登录成功
            if(status === 200){
                ctx.commit('SET_TOKEN', token)
                // 登录成功后。继续获取用户基本信息
                const {userInfo} = await getUserInfo()
                ctx.commit('SET_USERINFO', userInfo)
                return true
            }
            return false
        }
    },
}

export default userModule